#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
int t,n;
ll w[30];
int main(void){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        ll cnt=0;
        for(int i=1;i<=7;i++){
            scanf("%lld",&w[i]);
            cnt+=w[i];
            w[i+7]=w[i];
        }
        ll tmp=(n-1)/cnt;
        ll ans=7*tmp;
        ll rem=n-tmp*cnt;
        ll res=INF;
        for(int i=1;i<=7;i++){
            if(w[i]){
                int p=i;
                ll t=0;
                while(t<rem){
                    t+=w[p++];
                }
                res=min(res,ll(p-i));
            }
        }
        printf("%lld\n",ans+res);
    }
    return 0;
}